home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSINIT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-09-14
|
9KB
|
295 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bsinit.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to initialize the base library.
// UNIX and MSC should always call these routines.
// This routine registers an 'atexit' routine to do clean-up. This routine
// should be called if the application exits abnormally (execl,...).
//
// This module also computes executable load size under DOS and writes
// it to the file 'progsize.log'.
//
// The size of a program cannot be determined statically because the
// the stack and near heap are sized dynamically at run time. The
// load size specified in the EXE header is incorrect for this
// reason. The only way to accurately determine the program image
// size is to check it at run time.
//
// This program will get the current size of the program image in
// memory. This size will change dynamically as the program acquires
// memory from and releases memory to DOS. This in formation is
// stored in the Memory Control Block (MCB). The MCB begins one
// paragraph (16 bytes) before the Program Segment Prefix (PSP).
// The current program size is stored at offset 0x3 from the
// beginning of the MCB. Offset 0x3 represents the size in
// paragraphs therefore it must be multiplied by 16 to obtain the
// size in bytes.
//
// Environment variables
// __DEBUGGER__ Disable keyboard monitor and other interrupts
// while debugging
// LOG_LEVEL Set current logging level
// NO_KBD_MONITOR Disable keyboard monitor (Ctrl-C/Ctrl-Alt-Del)
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------
static BOOL fInit = FALSE;
static void BaseLibraryTerminate_Internal(void);
#if OS_WINDOWS // Instance handle for current window
static HANDLE hInstance = 0; // application
static HWND hwndMain = 0;
#endif
#define MAX_EXIT_FUNCS (32)
static SIZET cExitFuncs = 0;
static PFNEXIT apfnexit[MAX_EXIT_FUNCS];
static SIZET acExitPriority[MAX_EXIT_FUNCS];
//----------------------------------------------------------------------------
// Description: Register an exit function with the base library.
// This function is similar to the C library 'atexit' but
// is duplicated here so there is more control.
// Parameters:
//
// Higher priority functions are called last.
// Base library uses 128+
// Returns:
//----------------------------------------------------------------------------
BOOL FN_E BaseExitFunc(PFNEXIT pfnexit, SIZET cPriority)
{
SIZET i;
Assert(pfnexit);
for (i = 0; i < cExitFuncs; ++i) // Check duplicate functions
if (apfnexit[i] == pfnexit)
return TRUE;
if (cExitFuncs >= MAX_EXIT_FUNCS)
return Error("Maximum number of exit functions exceeded.");
apfnexit[cExitFuncs] = pfnexit;
acExitPriority[cExitFuncs] = cPriority;
cExitFuncs++;
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Base library initialize
// Parameters: argc, argv Parameters to main()
// hinst Windows startup parameters
// hPrevInst
// lpCmdLine
// nCmdShow
// Returns: void. This routines aborts to DOS if it fails during
// initialization.
//----------------------------------------------------------------------------
#if OS_WINDOWS
VOID FN_E BaseLibraryInitialize(HINSTANCE hinst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow, PBS_CFG pcfg)
#else
VOID FN_E BaseLibraryInitialize(int argc, char **argv, PBS_CFG pcfg)
#endif
{
if (fInit)
return ;
fInit = TRUE;
cExitFuncs = 0; // Reset exit function count
#if OS_WINDOWS // Instance handle for current window
hInstance = hinst; // Store instance globally
#endif
#if OS_WINDOWS==0 || COMPILE_DLL==0
atexit(BaseLibraryTerminate_Internal);
#endif
OutputInitialize();
// ClearStack();
HeapInitialize();
BaseExitFunc((PFNEXIT)FileTerminate, SYS_EXIT_PRIORITY);
#if OS_DOS
UmbSetState(1);
#endif
ProgramSizeShow("at start up");
#if OS_DOS
SleepInitialize();
#endif
#if OS_DOS || OS_OS2
KbdInitialize();
#endif
#if OS_WINDOWS
CommandLineInitialize(hinst, hPrevInst, lpCmdLine, nCmdShow);
#else
CommandLineInitialize(argc, argv);
#endif
#if OS_DOS && COMPILER_BORLAND
if (!DebugMode())
{
// CtrlCInitialize(); // Commented out to support Zinc!
CritErrInitialize();
}
#endif
LogOpen();
LogSetLevel((USHORT)EnvVal("LOG_LEVEL"));
Log(LOG_VERY_HIGH, "%s initialized.", CommandLineAppName());
if (pcfg) // Set default configuration
CfgSet(NULL, CFG_ALL, pcfg);
return ;
}
//----------------------------------------------------------------------------
// Description: Base library terminate.
// Parameters:
// Returns:
//----------------------------------------------------------------------------
VOID FN_E BaseLibraryTerminate(void)
{
BaseLibraryTerminate_Internal();
return ;
}
//----------------------------------------------------------------------------
// Description: Base library terminate. Internal -- called by atexit!
// NOTE: Assert() and Halt() exit directly to the OS using
// _exit(). Be sure to modify those routine to restore
// any modified interrupt handlers!
// Parameters:
// Returns:
//----------------------------------------------------------------------------
static void BaseLibraryTerminate_Internal(void)
{
BOOL fDone;
SIZET i, cPriority, cNext;
if (!fInit)
return;
ProgramSizeShow("at termination"); // Show terminal size
Log(LOG_VERY_HIGH, "%s terminating.", CommandLineAppName());
fInit = FALSE;
fDone = FALSE;
cPriority = 0;
while (!fDone) // Call exit functions in the proper order
{
cNext = MAX_SIZET;
fDone = TRUE;
for (i = cExitFuncs; i; --i) // Scan from last to first
{ // Watch for next exit level!
if (acExitPriority[i-1] > cPriority && acExitPriority[i-1] < cNext)
{
cNext = acExitPriority[i-1];
fDone = FALSE;
} // Call exit functions
if (acExitPriority[i-1] == cPriority)
(*apfnexit[i-1])();
}
cPriority = cNext;
}
return ;
}
//----------------------------------------------------------------------------
// Description:
// Parameters:
// Returns: Program instance handle
//----------------------------------------------------------------------------
#if OS_WINDOWS
HINSTANCE FN_E WindowsInstance(void)
{
return hInstance;
}
#endif
//----------------------------------------------------------------------------
// Description:
// Parameters:
// Returns: Program instance handle
//----------------------------------------------------------------------------
#if OS_WINDOWS
HWND FN_E WindowsMainWindow(void)
{
return hwndMain;
}
#endif
//----------------------------------------------------------------------------
// Description:
// Parameters:
// Returns: Program instance handle
//----------------------------------------------------------------------------
#if OS_WINDOWS
VOID FN_E WindowsSetMainWindow(HWND hwnd)
{
Assert(hwnd);
hwndMain = hwnd;
return ;
}
#endif
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------